﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using XMLGener.Utils;

namespace XMLGener.Utils
{
    /// <summary>
    /// This class is used as item, which contains information abour included file.
    /// </summary>
    public class TemplateFileInfo
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="path"></param>
        public TemplateFileInfo(string fileName, string path, XmlElement sourceElement)
        {
            absolutePath = DirectoriesUtil.combinePath(path, fileName);
            relativePath = fileName;
            this.sourceElement = sourceElement;
            FileInfo fi = new FileInfo(absolutePath);
            if (!fi.Exists)
            {
                info = "file does not exist";
                fileExists = false;
            }
            else
            {
                fileExists = true;
            }

        }

        public XmlElement sourceElement = null;
        
        /// <summary>
        /// Relative path of file
        /// </summary>
        public string relativePath = "";
        
        /// <summary>
        /// Absolute path of file
        /// </summary>
        public string absolutePath = "";
        
        /// <summary>
        /// Information about this file
        /// </summary>
        public string info = "";
        
        /// <summary>
        /// Indication, if this file exists.
        /// </summary>
        public bool fileExists = false;

        /// <summary>
        /// Return string, that represents this class
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if(!string.IsNullOrEmpty(info))
                return relativePath +" ("+info+")";
            return relativePath;

        }
        
        /// <summary>
        /// 
        /// </summary>
        public void changeRelativePath(DirectoryInfo newDir)
        {
            this.relativePath = DirectoriesUtil.getRelativePath(newDir, absolutePath);
            if (sourceElement != null)
                XMLUtils.setAttribute(sourceElement, "file", relativePath, null);                
            
            
        }

    }
}
